package net.alteiar.ui.editor.tools.params;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map.Entry;

import javax.swing.JPanel;

import net.alteiar.images.metadata.ImageInfo;
import net.alteiar.ui.editor.tools.ImageInfoTool;

public class MenuTool extends JPanel implements ActionListener {
	private static final long serialVersionUID = 1L;

	public enum EditorState {
		MOVE, BRUSH, FILL
	};

	private final HashMap<ToolUi, EditorState> tools;
	private EditorState state;
	private ImageInfoTool tool;

	public MenuTool(ImageInfo info, ImageInfo tmp) {
		tools = new HashMap<>();
		tools.put(new ToolUi("Brush", new BrushUi(info, tmp)),
				EditorState.BRUSH);
		tools.put(new ToolUi("Fill", new BrushUi(info, tmp)), EditorState.FILL);
		tools.put(new ToolUi("Test", new BrushUi(info, tmp)), EditorState.MOVE);

		this.setLayout(new FlowLayout());
		for (ToolUi ui : tools.keySet()) {
			ui.addActionListener(this);
			this.add(ui);
		}

		state = EditorState.MOVE;
	}

	private void unselectAll() {
		for (ToolUi ui : tools.keySet()) {
			ui.unselect();
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		unselectAll();
		for (Entry<ToolUi, EditorState> entry : tools.entrySet()) {
			if (entry.getKey().isSelected()) {
				state = entry.getValue();
				tool = entry.getKey().getTool();
			}
		}
	}

	public EditorState getState() {
		return this.state;
	}

	public ImageInfoTool getTool() {
		return this.tool;
	}
}
